/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.orm.testdesign;

import java.util.HashMap;
import java.util.LinkedList;
import org.orm.PersistentException;
import org.orm.PersistentTransaction;
import org.testis.orm.classes.T_test_designtree;
import org.testis.orm.classes.T_test_teststeps;
import org.testis.orm.manager.TestisDbPersistentManager;
import org.testis.orm.refinedclasses.TestDesignTreeNode;
import org.testis.orm.refinedclasses.TestDesignTreeRefined;
import org.testis.orm.refinedclasses.TestStepRefined;
import org.testis.orm.refinedclasses.TestDesignTreeRefined.TestDesignTreeParameters;
import org.testis.utils.middleware.TestDesign;

/**
 * 
 * @author nurhat
 */
public class TestStepDataHandler extends TestPartDataHandler {

    @Override
    public TestDesignTreeNode operationAdd(HashMap<String, Object> params,
	    TestDesignTreeNode parentNode, int childIndex) {
	TestDesignTreeNode newNode = null;
	String action = null;
	String name = null;
	String description = null;
	String modulType = null;
	String supportedDeviceGroup = null;

	name = (String) params.get("name");
	description = (String) params.get("description");
	action = (String) params.get("action");
	modulType = (String) params.get("modulType");
	supportedDeviceGroup = (String) params.get("supportedDeviceGroup");

	String aSynchCall = (String) params.get("aSynchCall");
	String browserSpeed = (String) params.get("browserSpeed");
	String browserType = (String) params.get("browserType");
	String critical = (String) params.get("critical");
	String dataArrayAppliesToCase = (String) params
		.get("dataArrayAppliesToCase");
	String delayTime = (String) params.get("delayTime");
	String dut = (String) params.get("dut");
	String nic = (String) params.get("nic");
	String noOfRetryOnFail = (String) params.get("noOfRetryOnFail");
	String pc = (String) params.get("pc");
	String successPercentage = (String) params.get("successPercentage");
	String timeout = (String) params.get("timeout");
	String owner = (String) params.get("owner");
	String deviceIp = (String) params.get("deviceIp");
	LinkedList<String> labels = (LinkedList<String>) params.get("labels");

	try {
	    PersistentTransaction t = TestisDbPersistentManager.instance()
		    .getSession().beginTransaction();
	    try {
		T_test_teststeps container;

		container = new T_test_teststeps();
		container.setName(name);
		container.setDescription(description);
		container.setModulTypeId(TestDesign
			.getModuleTypeIdByName(modulType));
		container.setActionId(TestDesign.getActionTypeIdByName(action));
		if (supportedDeviceGroup != null
			&& !(supportedDeviceGroup.equals(""))) {
		    container
			    .setSupportedDeviceGroupId(TestDesign
				    .getSupportedDeviceGroupIdByName(supportedDeviceGroup));
		} else {
		    container.setSupportedDeviceGroupId(null);
		}
		container.save();
		TestDesign.saveLabels(labels, container);

		T_test_designtree obj = new T_test_designtree();
		obj.save();
		TestDesignTreeRefined designTree = TestDesignTreeRefined
			.getDesignTree(obj);
		designTree.getTreeReference().setTypeId(
			TestDesign.getDesignPartTypeIdByName("TestStep"));
		designTree.getTreeReference().setParent(
			parentNode.getReference().getTreeReference());
		designTree.getTreeReference().setElementId(container.getId());
		designTree.getTreeReference().save();
		String moduleTypeName = TestDesign.getModuleTypeName(container
			.getModulTypeId());
		if (moduleTypeName.equals("WebAutomationModule")) {
		    if (browserType.equals("*")) {
			designTree.setParameterValue(
				TestDesignTreeParameters.BrowserType.name(),
				null);
			designTree.setParameterValue(
				TestDesignTreeParameters.BrowserSpeed.name(),
				null);
		    } else {
			designTree.setParameterValue(
				TestDesignTreeParameters.BrowserType.name(),
				browserType);
			designTree.setParameterValue(
				TestDesignTreeParameters.BrowserSpeed.name(),
				browserSpeed);
		    }
		    designTree.setParameterValue(
			    TestDesignTreeParameters.DeviceIp.name(), deviceIp);

		    if (!dut.equals("")) {
			designTree.setParameterValue(
				TestDesignTreeParameters.DeviceId.name(),
				TestDesign.getDutById(dut).getId());
		    } else {
			designTree.setParameterValue(
				TestDesignTreeParameters.DeviceId.name(), null);
		    }
		    designTree.setParameterValue(
			    TestDesignTreeParameters.NicId.name(), null);
		} else {
		    if (!nic.equals("") & !pc.equals("")) {
			designTree.setParameterValue(
				TestDesignTreeParameters.NicId.name(),
				TestDesign.getNicByName(nic, pc).getId());
		    }
		    designTree.setParameterValue(
			    TestDesignTreeParameters.DeviceId.name(), null);
		    designTree.setParameterValue(
			    TestDesignTreeParameters.BrowserType.name(), null);
		    designTree.setParameterValue(
			    TestDesignTreeParameters.BrowserSpeed.name(), null);
		    designTree.setParameterValue(
			    TestDesignTreeParameters.DeviceIp.name(), null);
		}
		Boolean asc = Boolean.parseBoolean(aSynchCall);
		designTree.setParameterValue(
			TestDesignTreeParameters.AsynchCall.name(), asc);
		if (asc) {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.NoOfRetryOnFail.name(), 0);
		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.NoOfRetryOnFail.name(),
			    noOfRetryOnFail);
		}
		designTree.setParameterValue(
			TestDesignTreeParameters.Critical.name(), critical);
		Boolean arrayAppliesToCase = Boolean
			.parseBoolean(dataArrayAppliesToCase);
		designTree.setParameterValue(
			TestDesignTreeParameters.DataArrayAppliestoCase.name(),
			arrayAppliesToCase);
		if (arrayAppliesToCase == false) {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.SuccessPercentage.name(),
			    Byte.parseByte(successPercentage));
		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.SuccessPercentage.name(),
			    null);

		}
		designTree.setParameterValue(
			TestDesignTreeParameters.DelayTime.name(),
			Integer.parseInt(delayTime));
		if (!pc.equals("")) {
		    designTree.setParameterValue(TestDesignTreeParameters.PcId
			    .name(), TestDesign.getPcByName(pc).getId());

		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.PcId.name(), null);
		}
		designTree.setParameterValue(
			TestDesignTreeParameters.Timeout.name(),
			Integer.parseInt(timeout));
		designTree.setParameterValue(
			TestDesignTreeParameters.OwnerId.name(),
			TestDesign.m_owners.get(owner).getId());
		designTree.getTreeReference().save();
		int place = TestDesign.setRankOfAdd(parentNode,
			designTree.getTreeReference(), childIndex);
		designTree.getTreeReference().save();
		t.commit();

		TestStepRefined result = new TestStepRefined(container);
		newNode = TestDesignTreeNode.getNode(designTree, result);
		if (place == -1) {
		    parentNode.addChild(newNode);
		} else {
		    parentNode.addChild(place, newNode);
		    newNode.setPlace(place);
		}
	    } catch (PersistentException ex) {
		t.rollback();
		ex.printStackTrace();
		newNode = null;
	    } finally {
		TestisDbPersistentManager.instance().disposePersistentManager();
	    }
	} catch (PersistentException ex) {
	    ex.printStackTrace();
	    newNode = null;
	}
	return newNode;
    }

    @Override
    public boolean operationSave(HashMap<String, Object> params,
	    TestDesignTreeNode node) {
	String name = (String) params.get("name");
	String description = (String) params.get("description");
	String action = (String) params.get("action");
	String modulType = (String) params.get("modulType");
	String aSynchCall = (String) params.get("aSynchCall");
	String critical = (String) params.get("critical");
	String dataArrayAppliesToCase = (String) params
		.get("dataArrayAppliesToCase");
	String delayTime = (String) params.get("delayTime");

	// Dynamic Parameters
	String browserSpeed = (String) params.get("browserSpeed");
	String browserType = (String) params.get("browserType");
	String deviceIp = (String) params.get("deviceIp");
	String dut = (String) params.get("dut");
	String nic = (String) params.get("nic");
	String pc = (String) params.get("pc");
	// End Dynamic Parameters

	String noOfRetryOnFail = (String) params.get("noOfRetryOnFail");
	String successPercentage = (String) params.get("successPercentage");
	String supportedDeviceGroup = (String) params
		.get("supportedDeviceGroup");
	String timeout = (String) params.get("timeout");
	LinkedList<String> labels = (LinkedList<String>) params.get("labels");
	String dynamicSaveOnly = (String) params.get("dynamicSaveOnly");

	try {
	    PersistentTransaction t = TestisDbPersistentManager.instance()
		    .getSession().beginTransaction();
	    TestStepRefined stepObject = (TestStepRefined) node.getObject();
	    try {
		String oldName;
		T_test_teststeps container = stepObject.getReference();
		TestDesignTreeRefined designTree = node.getReference();
		oldName = container.getName();
		if (dynamicSaveOnly == null) {
		    container.setName(name);
		    container.setDescription(description);
		    if (supportedDeviceGroup != null
			    && !(supportedDeviceGroup.equals(""))) {
			container
				.setSupportedDeviceGroupId(TestDesign
					.getSupportedDeviceGroupIdByName(supportedDeviceGroup));
		    } else {
			container.setSupportedDeviceGroupId(null);
		    }
		    if (modulType != null) {
			container.setModulTypeId(TestDesign
				.getModuleTypeIdByName(modulType));
		    }
		    if (action != null) {
			container.setActionId(TestDesign
				.getActionTypeIdByName(action));
		    }

		    container.save();
		}
		TestDesign.saveLabels(labels, container);
		String moduleTypeName = TestDesign.getModuleTypeName(container
			.getModulTypeId());
		if (moduleTypeName.equals("WebAutomationModule")) {
		    if (browserType.equals("*")) {
			designTree.setParameterValue(
				TestDesignTreeParameters.BrowserType.name(),
				null);
			designTree.setParameterValue(
				TestDesignTreeParameters.BrowserSpeed.name(),
				null);
		    } else {
			designTree.setParameterValue(
				TestDesignTreeParameters.BrowserType.name(),
				browserType);
			designTree.setParameterValue(
				TestDesignTreeParameters.BrowserSpeed.name(),
				browserSpeed);
		    }
		    designTree.setParameterValue(
			    TestDesignTreeParameters.DeviceIp.name(), deviceIp);

		    if (!dut.equals("")) {
			designTree.setParameterValue(
				TestDesignTreeParameters.DeviceId.name(),
				TestDesign.getDutById(dut).getId());
		    } else {
			designTree.setParameterValue(
				TestDesignTreeParameters.DeviceId.name(), null);
		    }
		    designTree.setParameterValue(
			    TestDesignTreeParameters.NicId.name(), null);
		} else {
		    if (!nic.equals("") & !pc.equals("")) {
			designTree.setParameterValue(
				TestDesignTreeParameters.NicId.name(),
				TestDesign.getNicByName(nic, pc).getId());
		    }
		    designTree.setParameterValue(
			    TestDesignTreeParameters.DeviceId.name(), null);
		    designTree.setParameterValue(
			    TestDesignTreeParameters.BrowserType.name(), null);
		    designTree.setParameterValue(
			    TestDesignTreeParameters.BrowserSpeed.name(), null);
		    designTree.setParameterValue(
			    TestDesignTreeParameters.DeviceIp.name(), null);
		}
		Boolean asc = Boolean.parseBoolean(aSynchCall);
		designTree.setParameterValue(
			TestDesignTreeParameters.AsynchCall.name(), asc);
		if (asc) {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.NoOfRetryOnFail.name(), 0);
		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.NoOfRetryOnFail.name(),
			    noOfRetryOnFail);
		}
		designTree.setParameterValue(
			TestDesignTreeParameters.Critical.name(), critical);
		Boolean arrayAppliesToCase = Boolean
			.parseBoolean(dataArrayAppliesToCase);
		designTree.setParameterValue(
			TestDesignTreeParameters.DataArrayAppliestoCase.name(),
			arrayAppliesToCase);
		if (arrayAppliesToCase == false) {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.SuccessPercentage.name(),
			    Byte.parseByte(successPercentage));
		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.SuccessPercentage.name(),
			    null);

		}
		designTree.setParameterValue(
			TestDesignTreeParameters.DelayTime.name(),
			Integer.parseInt(delayTime));
		if (!pc.equals("")) {
		    designTree.setParameterValue(TestDesignTreeParameters.PcId
			    .name(), TestDesign.getPcByName(pc).getId());

		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.PcId.name(), null);
		}
		designTree.setParameterValue(
			TestDesignTreeParameters.Timeout.name(),
			Integer.parseInt(timeout));
		designTree.getTreeReference().save();

		t.commit();

		return true;
	    } catch (PersistentException ex) {
		t.rollback();
	    } finally {
		TestisDbPersistentManager.instance().disposePersistentManager();
	    }
	} catch (PersistentException ex) {
	}
	return false;
    }
}
